package com.donfu.web.controller.house;

import java.util.Date;
import java.util.List;

import com.donfu.system.domain.house.HouseNotice;
import com.donfu.system.domain.house.HouseStore;
import com.donfu.system.domain.house.HouseStoreJoin;
import com.donfu.system.service.house.IHouseNoticeService;
import com.donfu.system.service.house.IHouseStoreJoinService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.donfu.common.annotation.Log;
import com.donfu.common.core.controller.BaseController;
import com.donfu.common.core.domain.AjaxResult;
import com.donfu.common.enums.BusinessType;
import com.donfu.common.core.page.TableDataInfo;

/**
 * 店铺加盟Controller
 * 
 * @author donfu
 * @date 2021-12-08
 */
@Api(tags = "后台店铺加盟管理")
@RestController
@RequestMapping("/house/join")
public class HouseStoreJoinController extends BaseController
{
    @Autowired
    private IHouseStoreJoinService houseStoreJoinService;

    @Autowired
    private IHouseNoticeService houseNoticeService;

    /**
     * 查询店铺加盟列表
     */
    @ApiOperation("查询店铺加盟列表")
    @PreAuthorize("@ss.hasPermi('house:join:list')")
    @GetMapping("/list")
    public TableDataInfo list(HouseStoreJoin houseStoreJoin)
    {
        startPage();
        List<HouseStoreJoin> list = houseStoreJoinService.selectHouseStoreJoinList(houseStoreJoin);
        return getDataTable(list);
    }


    /**
     * 获取店铺加盟详细信息
     */
    @ApiOperation("查询详情")
    @PreAuthorize("@ss.hasPermi('house:join:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(houseStoreJoinService.selectHouseStoreJoinById(id));
    }

    /**
     * 新增店铺加盟
     */
    @ApiOperation("新增店铺加盟")
    @PreAuthorize("@ss.hasPermi('house:join:add')")
    @Log(title = "店铺加盟", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HouseStoreJoin houseStoreJoin)
    {
        Long userId = houseStoreJoin.getUserId();
        // 查询加盟表是否有此数据
        HouseStoreJoin join=new HouseStoreJoin();
        join.setUserId(userId);
        List<HouseStoreJoin> houseStoreJoinList = houseStoreJoinService.selectHouseStoreJoinList(join);
        if(houseStoreJoinList!=null && houseStoreJoinList.size()>0){
            return AjaxResult.error("此用户id已加盟");
        }
        return toAjax(houseStoreJoinService.insertHouseStoreJoin(houseStoreJoin));
    }

    /**
     * 修改店铺加盟
     */
    @ApiOperation("修改店铺加盟")
    @PreAuthorize("@ss.hasPermi('house:join:edit')")
    @Log(title = "店铺加盟", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseStoreJoin houseStoreJoin)
    {
        if(houseStoreJoin.getId()==null){
            return AjaxResult.error("id不能为空");
        }
        return toAjax(houseStoreJoinService.updateHouseStoreJoin(houseStoreJoin));
    }

    /**
     * 后台审核店铺加盟
     * @param houseStoreJoin
     * @return
     */
    @ApiOperation("审核店铺加盟")
    @PostMapping("/audit")
    public AjaxResult auditStoreJoin(@RequestBody HouseStoreJoin houseStoreJoin){
        Integer isAllow = houseStoreJoin.getIsAllow();
        int i = houseStoreJoinService.updateHouseStoreJoin(houseStoreJoin);
        if(i>0) {
            if (isAllow == 1) {
                // 如果审核通过，那么就发审核通过信息到前台
                HouseNotice houseNotice = new HouseNotice();
                houseNotice.setTitle("加盟通过通知");
                houseNotice.setCreateDate(new Date());
                houseNotice.setUserId(houseStoreJoin.getUserId());
                houseNotice.setContent("尊敬的" + houseStoreJoin.getUserName() + "用户,恭喜您成功加盟" + houseStoreJoin.getStoreName() + "！期待我们有良好的合作,欢迎您的指导！");
                houseNoticeService.insertHouseNotice(houseNotice);
            } else if (isAllow == 2) {
                // 如果审核未通过，那么也应该发送通知，告诉对方
                HouseNotice houseNotice = new HouseNotice();
                houseNotice.setTitle("加盟失败通知");
                houseNotice.setCreateDate(new Date());
                houseNotice.setUserId(houseStoreJoin.getUserId());
                houseNotice.setContent("尊敬的" + houseStoreJoin.getUserName() + "用户,您加盟" + houseStoreJoin.getStoreName() + "失败！我们感到非常抱歉！");
                houseNoticeService.insertHouseNotice(houseNotice);
            }
        }
        return toAjax(i);
    }

    /**
     * 删除店铺加盟
     */
    @ApiOperation("删除店铺加盟")
    @PreAuthorize("@ss.hasPermi('house:join:remove')")
    @Log(title = "店铺加盟", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(houseStoreJoinService.deleteHouseStoreJoinByIds(ids));
    }


}
